/*
 *所有关于float_white_list类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*float_white_listService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using System.Collections.Generic;
using VOL.Core.ManageUser;
using VOL.Core.DBManager;
using System.ComponentModel.DataAnnotations;
using System;

namespace VOL.System.Services
{
    public partial class float_white_listService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ifloat_white_listRepository _repository;//访问数据库
        private WebResponseContent webResponse = new WebResponseContent();

        [ActivatorUtilitiesConstructor]
        public float_white_listService(
            Ifloat_white_listRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<float_white_list> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<float_white_list> queryable) =>
            {
                if (options.Value == null)
                    return queryable;
                else
                {
                    var user = UserContext.Current.GetUserInfo(UserContext.Current.UserId);
                    if (user.UserTrueName != null) 
                        queryable = queryable.Where(x => user.Role_Id == 1 || user.Role_Id == 2 || user.User_Id==x.user_id);//数据隔离
                    return queryable;
                }
            };
            return base.GetPageData(options);
        }

        public override WebResponseContent Add(SaveModel saveModel)
        {
            string float_id = "";
            string float_node_id="";
            AddOnExecute = (SaveModel saveModel) =>
            {
                foreach (var item in saveModel.MainData)
                {
                    if (item.Key == "float_id")
                    {
                        float_id = item.Value.ToString();
                    }
                    if (item.Key == "float_node_id")
                    {
                        float_node_id = item.Value.ToString();
                    }
                }
                return webResponse.OK();
            };
            AddOnExecuting = (float_white_list fwl, object list) =>
            {

                if (repository.Exists(x => x.float_id == fwl.float_id && x.float_node_id == fwl.float_node_id))
                {
                    return webResponse.Error("该流程节点白名单已由其他人维护！");
                }

                List<float_white_list_user> userList = list as List<float_white_list_user>;
                if(!(userList == null || userList.Count == 0))
                {
                    if(userList.Exists(x => x.user_id.ToString().Length == 0 && x.note.Length > 0))
                    {
                        return webResponse.Error("用户名必填！");
                    }
                    userList.ForEach(x =>
                    {
                        x.float_id = float_id;
                        x.float_node_id = float_node_id;
                        string floatSql = "select ft_id,ft_table from fl_template where ft_id='" + float_id + "';";
                        string floatNodeSql = "select ft_list_id  ,stepid from fl_template_list where ft_list_id ='" + float_node_id + "';";
                        List<dynamic> floatList = DBServerProvider.SqlDapper.QueryList<dynamic>(floatSql, null).ToList();
                        List<dynamic> floatNodeList = DBServerProvider.SqlDapper.QueryList<dynamic>(floatNodeSql, null).ToList();
                        floatList.ForEach(item =>{x.ft_table = item.ft_table; });
                        floatNodeList.ForEach(item => { x.step_id = item.stepid; });
                    });
                }
                return webResponse.OK();
            };
            return base.Add(saveModel);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            string float_id = "";
            string float_node_id = "";
            UpdateOnExecute =(SaveModel saveModel)=>{
                foreach (var item in saveModel.MainData)
                {
                    if (item.Key == "float_id")
                    {
                        float_id = item.Value.ToString();
                    }
                    if (item.Key == "float_node_id")
                    {
                        float_node_id = item.Value.ToString();
                    }
                }
                return webResponse.OK();
            };
            UpdateOnExecuting = (float_white_list fwl, object list, object updateList, List<object> delKeys) =>
            {
                List<float_white_list_user> userList = list as List<float_white_list_user>;
                if (userList.Exists(x => x.user_id.ToString().Length == 0 && x.note.Length > 0))
                {
                    return webResponse.Error("用户名必填！");
                }
                userList.ForEach(x =>
                {
                    x.float_id = float_id;
                    x.float_node_id = float_node_id;
                    string floatSql = "select ft_id,ft_table from fl_template where ft_id='" + float_id + "';";
                    string floatNodeSql = "select ft_list_id  ,stepid from fl_template_list where ft_list_id ='" + float_node_id + "';";
                    List<dynamic> floatList = DBServerProvider.SqlDapper.QueryList<dynamic>(floatSql, null).ToList();
                    List<dynamic> floatNodeList = DBServerProvider.SqlDapper.QueryList<dynamic>(floatNodeSql, null).ToList();
                    floatList.ForEach(item => { x.ft_table = item.ft_table; });
                    floatNodeList.ForEach(item => { x.step_id = item.stepid; });
                });

                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
    }
}
